//
//  ZLTapViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "ZLTapViewController.h"
#import "ZLTapCollectionViewCell.h"
#import "WJQNetwork.h"
#import "ZLTapModel.h"
#import "ZLPhotoScrollViewController.h"


@interface ZLTapViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UICollectionView *myCollectionView;

@property (nonatomic, strong)NSMutableArray *photoArray;

@property (nonatomic, assign)NSInteger page;

@end

@implementation ZLTapViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = [self.string stringByAppendingString:@"图片"];
//    self.imageArr = [NSMutableArray array];
    self.photoArray = [NSMutableArray array];
    self.page = 0;
    [self createCollectionView];
    [self MBShow];
    [self getData:self.page];
    [self addFooterRefresh];
}

#pragma mark - 上拉加载
- (void)addFooterRefresh
{
    self.myCollectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.page++;
        NSLog(@"上拉");
        [self getData:self.page];
    }];
}

#pragma mark - 创建CollectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.minimumInteritemSpacing = 5*AUTO_HEIGHT;
    flowLayout.minimumLineSpacing = 5 * AUTO_WIDTH;
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    [self.view addSubview:self.myCollectionView];
    
    //注册
    [self.myCollectionView registerClass:[ZLTapCollectionViewCell class] forCellWithReuseIdentifier:@"ZLTapCollectionViewCell"];
    
//    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionReusableView"];
    
}

#pragma mark - CollectionView协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLTapCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLTapCollectionViewCell" forIndexPath:indexPath];
    ZLTapModel *zlTapM = [self.photoArray objectAtIndex:indexPath.row];
    cell.zlTapM = zlTapM;
    return cell;
}

#pragma mark - item高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH - 10*AUTO_WIDTH)/3,(SCREEN_WIDTH - 10*AUTO_WIDTH)/3);
}

#pragma mark - 尾部
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    if (kind == UICollectionElementKindSectionFooter) {
//        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
//        
//        return reusableView;
//    }
//    return nil;
//}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return CGSizeMake(SCREEN_WIDTH, 30*AUTO_HEIGHT);
//}


#pragma mark - 请求数据
- (void)getData:(NSInteger)page
{
    
    //http://api.breadtrip.com//destination/place/2/6636/photos/?gallery_mode=1&count=18&sign=f708263fd728e64d751824abda462547
    
    
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%ld",self.type,self.myId,self.page*18];
    
    [WJQNetwork getAFNet:str block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"items"];
        for (NSMutableDictionary *Dic in array) {
            ZLTapModel *zlTapM = [ZLTapModel new];
            [zlTapM setValuesForKeysWithDictionary:Dic];
            [self.photoArray addObject:zlTapM];
        }
        [self.myCollectionView.mj_footer endRefreshing];
        [self.myCollectionView reloadData];
        [self MBHide];
    } errorBlock:^(NSError *error) {
        
        NSLog(@"请求失败");
    }];
    
}

#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLPhotoScrollViewController *photoSVC = [ZLPhotoScrollViewController new];
    ZLTapModel *zlTapM = [self.photoArray objectAtIndex:indexPath.row];
    photoSVC.zlTapM = zlTapM;
    photoSVC.myArray = [NSMutableArray arrayWithArray:self.photoArray];
    photoSVC.num = indexPath.row;
    [self.navigationController pushViewController:photoSVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
